using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Models;

namespace DAL
{
    public static partial class AdminLogService
    {
        public static bool AddAdminLog(AdminLog adminLog)
        {
            string sql =
                "INSERT AdminLog (AdminName, LogDesc, LogTime)" +
                "VALUES (@AdminName, @LogDesc, @LogTime)";

            sql += " ; SELECT @@IDENTITY";


            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@AdminName", adminLog.AdminName),
				new SqlParameter("@LogDesc", adminLog.LogDesc),
				new SqlParameter("@LogTime", adminLog.LogTime)
			};

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }


        public static bool DeleteAdminLogByLogID(int logID)
        {
            string sql = "DELETE AdminLog WHERE LogID = @LogID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@LogID", logID)
			};

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public static bool DeleteAdminLog()
        {
            string sql = "DELETE AdminLog WHERE LogID > 0";

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, null);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        public static bool ModifyAdminLog(AdminLog adminLog)
        {
            string sql =
                "UPDATE AdminLog " +
                "SET " +
                    "AdminName = @AdminName, " +
                    "LogDesc = @LogDesc, " +
                    "LogTime = @LogTime " +
                "WHERE LogID = @LogID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@LogID", adminLog.LogID),
				new SqlParameter("@AdminName", adminLog.AdminName),
				new SqlParameter("@LogDesc", adminLog.LogDesc),
				new SqlParameter("@LogTime", adminLog.LogTime)
			};

            int i = DBHelper.ExecuteNonQuery(CommandType.Text, sql, para);
            if (i > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public static IList<AdminLog> GetAllAdminLogs()
        {
            string sqlAll = "SELECT * FROM AdminLog Order by LogID desc";
            return GetAdminLogsBySql(sqlAll);
        }

        public static AdminLog GetAdminLogByLogID(int logID)
        {
            string sql = "SELECT * FROM AdminLog WHERE LogID = @LogID";

            SqlParameter[] para = new SqlParameter[]
			{
				new SqlParameter("@LogID", logID)
			};

            DataTable temp = DBHelper.ExecuteGetDataTable(CommandType.Text, sql, para);

            AdminLog adminLog = new AdminLog();

            foreach (DataRow dr in temp.Rows)
            {
                adminLog.LogID = (int)dr["LogID"];
                adminLog.AdminName = (string)dr["AdminName"];
                adminLog.LogDesc = (string)dr["LogDesc"];
                adminLog.LogTime = (DateTime)dr["LogTime"];

            }
            return adminLog;

        }


        private static IList<AdminLog> GetAdminLogsBySql(string safeSql)
        {
            List<AdminLog> list = new List<AdminLog>();

            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, null))
            {
                while (dr.Read())
                {
                    AdminLog adminLog = new AdminLog();

                    adminLog.LogID = (int)dr["LogID"];
                    adminLog.AdminName = (string)dr["AdminName"];
                    adminLog.LogDesc = (string)dr["LogDesc"];
                    adminLog.LogTime = (DateTime)dr["LogTime"];

                    list.Add(adminLog);
                }
            }
            return list;
        }

        private static IList<AdminLog> GetAdminLogsBySql(string safeSql, params SqlParameter[] para)
        {
            List<AdminLog> list = new List<AdminLog>();

            using (SqlDataReader dr = DBHelper.ExecuteGetReader(CommandType.Text, safeSql, para))
            {
                while (dr.Read())
                {
                    AdminLog adminLog = new AdminLog();

                    adminLog.LogID = (int)dr["LogID"];
                    adminLog.AdminName = (string)dr["AdminName"];
                    adminLog.LogDesc = (string)dr["LogDesc"];
                    adminLog.LogTime = (DateTime)dr["LogTime"];

                    list.Add(adminLog);
                }
            }
            return list;
        }
    }
}
